--[[
    Author          ZhengFeng
    Time            2020.12.05
    Description     宠物融合界面-查看天赋数值列表
]]--

require "Common/define"
require "Logic/PetManager"
local Page_PetMergeTalentList = UIBase.New(PanelResNames.Page_PetMergeTalentList)
local this = Page_PetMergeTalentList
local listIndex = 0
local talentList = {}       --牧场等级
local talentEvoList = {}    --飞飞等级
local talentType = 0--0牧场等级 1飞飞等级
local ActiveType = {
    Active = 0,
    Inactive = 1,
}

function this.Init(type,idx)
    talentType = type;
    UIBase.Init(this)
end

function this:OnCreate()
    self.Btn_BGClose = self:GetChild("Btn_BGClose")
    self.bgClose = self:GetChild("bgClose")
    self.List_Talent = self:GetChild("List_Talent")
    self.List_Talent.itemRenderer = this.rendererTalentListItem
    self.Txt_Title = self:GetChild("Txt_Title")
    self.levelText = self:GetChild("level");
    self.typeController = self:GetController("type")

    self:RegisterOnClick(self.Btn_BGClose, self.onClickClose)
    self:RegisterOnClick(self.bgClose, self.onClickClose)
end

function this:OnEnable()
    talentList = {}
    local _talentId
    local _talentItem
    local _talentInfo
    local t_elvesmerge = G.dataTable["t_elvesmerge"]
    local t_elvestalent = G.dataTable["t_elvestalent"]

    local t_elvesevolevel = G.dataTable["t_elvesevolevel"]

    if talentType == 0 then
        self.levelText.text = PetManager.petLv;
        for i = PetManager.MaxPetLevel, 1, -1 do
            _talentId = t_elvesmerge[tostring(i)].f_TalentUnlock
            _talentItem = t_elvestalent[tostring(_talentId)]
            _talentInfo = { level = i, talentItem = _talentItem }
            table.insert(talentList, _talentInfo)
        end
        table.sort(talentList, function(a, b)
            return a.level < b.level
        end)
    else
        self.levelText.text = PetManager.EvoLevel;
        for i = 1, PetManager.MaxPetElvLevel do
            _talentId = t_elvesevolevel[tostring(i)].f_Level
            _talentInfo = { level = _talentId, talentItem = t_elvesevolevel[tostring(i)] }
            table.insert(talentList, _talentInfo)
        end
    end
    self.typeController.selectedIndex = talentType;
    this:showTalentList()
end

function this:OnDisable()

end
function this:OnDestroy()
    talentList = {}
end

function this:onClickClose()

    closeUI(PanelResNames.Page_PetMergeTalentList)
end

function this:showTalentList()
    self.List_Talent.numItems = #talentList
    listIndex = 0
    HelperFunc.ListTranstionPlay(self.List_Talent)
    local _scrollIndex = PetManager.MaxPetLevel - PetManager.petLv
    if _scrollIndex >= #talentList then
        _scrollIndex = #talentList - 1
    elseif _scrollIndex < 0 then
        _scrollIndex = 0
    end
    --self.List_Talent:ScrollToView(_scrollIndex)
end

function this:rendererTalentListItem(obj)
    listIndex = listIndex + 1
    local _talentItem = {};
    _talentItem = talentList[listIndex].talentItem

    --local Img_Arrow = obj:GetChild("Img_Arrow")
    --Img_Arrow.visible = listIndex > 1
    --local Img_TalentIcon = obj:GetChild("Img_TalentIcon")
    --Img_TalentIcon.url = PetManager.GetTalentBuffIcon(_talentItem.f_TalentBuffType)
    local Txt_TalentName = obj:GetChild("Txt_TalentName")
    if _talentItem.f_TalentName then
        Txt_TalentName.text = getLanguage(_talentItem.f_TalentName)
    end
    local Txt_TalentDesc = obj:GetChild("Txt_TalentDesc")
    Txt_TalentDesc.text = getLanguage(_talentItem.f_TalentDesc)
    local _controller = obj:GetController("ActiveType")
    local _selectedIndex = ActiveType.Active
    if talentType == 0 then

        if talentList[listIndex].level > PetManager.petLv then
            _selectedIndex = ActiveType.Inactive
        end
    else
        if talentList[listIndex].level > PetManager.EvoLevel then
            _selectedIndex = ActiveType.Inactive
        end
    end
    _controller.selectedIndex = _selectedIndex
    local _unlockPattern = getLanguage("Sys_ElvesTalentUnlockLevel")
    local Txt_UnlockLevel = obj:GetChild("Txt_UnlockLevel")
    Txt_UnlockLevel.text = string.format(getLanguage("Level"), talentList[listIndex].level);
end

return this